package com.chencs.app.category.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.app.AppCommon;
import com.chencs.app.category.service.AppCategoryService;
import com.chencs.vo.biz.category.CategoryModel;
import com.chencs.vo.biz.product.KeyModel;
import com.chencs.vo.net.request.CategoryRequest;
import com.chencs.vo.net.request.RegisterRequest;
import com.chencs.vo.net.request.RequestBase;
import com.chencs.vo.net.response.CategoryResponse;
import com.chencs.vo.net.response.KeyResponse;
import com.chencs.vo.net.response.ResponseBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by fangzehua on 2017/7/11.
 */


@RestController
@RequestMapping("/app/category")
public class AppCategoryController   {

    @Autowired
    private AppCategoryService appCategoryService;

    /**
     *
     * @return
     */
    @RequestMapping(value = "/categoryList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getCategoryList(@RequestBody CategoryRequest request){
        CategoryResponse response = new CategoryResponse();
        List<CategoryModel> list = appCategoryService.getCategoryList(request);
        try {
            if(!list.isEmpty()){
                response.setCode(AppCommon.SUCCESS);
                response.setCategorys(list);
            }else{
                response.setCode(AppCommon.NULL);
            }
        }catch (Exception e){
            response.setCode(AppCommon.ERROR);
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/getAllCategoryList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getAllCategoryList(@RequestBody CategoryRequest request){
        CategoryResponse response = new CategoryResponse();
        List<CategoryModel> list = appCategoryService.getAllCategoryList(request);
            try {
                if(!list.isEmpty()){
                    response.setCode(AppCommon.SUCCESS);
                    response.setCategorys(list);
                }else{
                    response.setCode(AppCommon.NULL);
                }
            }catch (Exception e){
                e.printStackTrace();
                response.setCode(AppCommon.ERROR);
            }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/getOtherCategoryList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getOtherCategoryList(@RequestBody CategoryRequest request){
        CategoryResponse response = new CategoryResponse();
        List<CategoryModel> list = appCategoryService.getOtherCategoryList(request);
        response.setCategorys(list);
       // System.out.println(JSON.toJSONString(response));
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/keyList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getkeyList(@RequestBody RequestBase request){
        KeyResponse response = new KeyResponse();
        List<KeyModel> list = appCategoryService.getKeyList(request);
        response.setList(list);
   //     System.out.println(JSON.toJSONString(response));
        return JSON.toJSONString(response);
    }


}
